package com.hp.bon.sgw.domain;

import com.hp.bon.sgw.util.Comutils;

import javax.persistence.*;
import java.sql.Timestamp;

@Entity
@Table(name = "t_business_record")
public class BusinessRecord {
	private int id;
	private String businessCallId;
	private String sessionId;
	private String serviceContextId;
	private String requestHost; // 请求端HOST
	private String requestIP;// 请求端IP
	private int fromNodeId;
	private int toNodeId;
	private int protocolType;
	private int transDirection;
	private Timestamp requestTime;
	private Timestamp responseTime;
	private int result;
	private String resultReason;
	private long recvRequestTime;
	private long sendRequestTime;
	private long recvResponseTime;
	private long sendResponseTime;
	private int recvRequestLength;
	private int sendRequestLength;
	private int recvResponseLength;
	private int sendResponseLength;
	private String responseHost;
	private Integer resultCode; // DCC协议结果码
	private Integer serviceResultCode;// 业务结果码

	private Integer answerValidateCode = -1;
	private String validateDesc;
	public static int seq = 0;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return id;
	}

	@Column(name = "business_call_id")
	public String getBusinessCallId() {
		return businessCallId;
	}

	@Column(name = "session_id")
	public String getSessionId() {
		return sessionId;
	}

	@Column(name = "service_context_id")
	public String getServiceContextId() {
		return serviceContextId;
	}

	@Column(name = "request_host")
	public String getRequestHost() {
		return requestHost;
	}

	@Column(name = "request_ip")
	public String getRequestIP() {
		return requestIP;
	}

	@Column(name = "from_node_id")
	public int getFromNodeId() {
		return fromNodeId;
	}

	@Column(name = "to_node_id")
	public int getToNodeId() {
		return toNodeId;
	}

	@Column(name = "protocol_type")
	public int getProtocolType() {
		return protocolType;
	}

	@Column(name = "trans_direction")
	public int getTransDirection() {
		return transDirection;
	}

	@Column(name = "result_code")
	public Integer getResultCode() {
		return resultCode;
	}

	@Column(name = "service_result_code")
	public Integer getServiceResultCode() {
		return serviceResultCode;
	}

	@Column(name = "result_reason")
	public String getResultReason() {
		return resultReason;
	}

	@Column(name = "request_time")
	public Timestamp getRequestTime() {
		return requestTime;
	}

	@Column(name = "response_time")
	public Timestamp getResponseTime() {
		return responseTime;
	}

	@Column(name = "recv_request_time")
	public long getRecvRequestTime() {
		return this.recvRequestTime;
	}

	@Column(name = "send_request_time")
	public long getSendRequestTime() {
		return sendRequestTime;
	}

	@Column(name = "recv_response_time")
	public long getRecvResponseTime() {
		return recvResponseTime;
	}

	@Column(name = "send_response_time")
	public long getSendResponseTime() {
		return sendResponseTime;
	}

	@Column(name = "recv_request_length")
	public int getRecvRequestLength() {
		return recvRequestLength;
	}

	@Column(name = "send_request_length")
	public int getSendRequestLength() {
		return sendRequestLength;
	}

	@Column(name = "recv_response_length")
	public int getRecvResponseLength() {
		return recvResponseLength;
	}

	@Column(name = "send_response_length")
	public int getSendResponseLength() {
		return sendResponseLength;
	}

	@Column(name = "response_host")
	public String getResponseHost() {
		return responseHost;
	}

	@Column(name = "ack_validate_code")
	public Integer getAnswerValidateCode() {
		return answerValidateCode;
	}

	@Column(name = "ack_validate_desc")
	public String getValidateDesc() {
		return validateDesc;
	}

	public void setAnswerValidateCode(Integer answerValidateCode) {
		this.answerValidateCode = answerValidateCode;
	}

	public void setValidateDesc(String validateDesc) {
		this.validateDesc = validateDesc;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setBusinessCallId(String businessCallId) {
		this.businessCallId = businessCallId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public void setServiceContextId(String serviceContextId) {
		this.serviceContextId = serviceContextId;
	}

	public void setRequestHost(String requestHost) {
		this.requestHost = requestHost;
	}

	public void setRequestIP(String requestIP) {
		this.requestIP = requestIP;
	}

	public void setFromNodeId(int fromNodeId) {
		this.fromNodeId = fromNodeId;
	}

	public void setToNodeId(int toNodeId) {
		this.toNodeId = toNodeId;
	}

	public void setProtocolType(int protocolType) {
		this.protocolType = protocolType;
	}

	public void setTransDirection(int transDirection) {
		this.transDirection = transDirection;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

	public void setServiceResultCode(Integer serviceResultCode) {
		this.serviceResultCode = serviceResultCode;
	}

	public void setResultReason(String resultReason) {
		this.resultReason = resultReason;
	}

	public void setRequestTime(Timestamp requestTime) {
		this.requestTime = requestTime;
	}

	public void setResponseTime(Timestamp responseTime) {
		this.responseTime = responseTime;
	}

	public void setRecvRequestTime(long recvRequestTime) {
		this.recvRequestTime = recvRequestTime;

		setRequestTime(new Timestamp(recvRequestTime));
	}

	public void setSendRequestTime(long sendRequestTime) {
		this.sendRequestTime = sendRequestTime;
	}

	public void setRecvResponseTime(long recvResponseTime) {
		this.recvResponseTime = recvResponseTime;
	}

	public void setSendResponseTime(long sendResponseTime) {
		this.sendResponseTime = sendResponseTime;
		setResponseTime(new Timestamp(sendResponseTime));
	}

	public void setRecvRequestLength(int recvRequestLength) {
		this.recvRequestLength = recvRequestLength;
	}

	public void setSendRequestLength(int sendRequestLength) {
		this.sendRequestLength = sendRequestLength;
	}

	public void setRecvResponseLength(int recvResponseLength) {
		this.recvResponseLength = recvResponseLength;
	}

	public void setSendResponseLength(int sendResponseLength) {
		this.sendResponseLength = sendResponseLength;
	}

	public void setResponseHost(String responseHost) {
		this.responseHost = responseHost;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public String toString() {
		return this.businessCallId + "||||" + this.sessionId + "||||" + this.serviceContextId + "||||" + this.requestHost + "||||" + this.requestIP + "||||" + this.fromNodeId + "||||" + this.toNodeId + "||||" + this.protocolType + "||||" + this.transDirection + "||||" + this.result + "||||"
				+ this.resultReason + "||||" + this.recvRequestTime + "||||" + this.sendRequestTime + "||||" + this.recvResponseTime + "||||" + this.sendResponseTime + "||||" + this.recvRequestLength + "||||" + this.sendRequestLength + "||||" + this.recvResponseLength + "||||"
				+ this.sendResponseLength + "||||" + this.responseHost + "||||" + this.resultCode + "||||" + this.serviceResultCode + "||||" + this.answerValidateCode + "||||" + this.validateDesc;
	}

	public static BusinessRecord convertToObject(String str) {
		String[] values = str.split("\\|\\|\\|\\|");
		BusinessRecord br = new BusinessRecord();
		br.setBusinessCallId(values[0]);
		br.setSessionId(values[1]);
		br.setServiceContextId(values[2]);
		br.setRequestHost(values[3]);
		br.setRequestIP(values[4]);
		br.setFromNodeId(getInt(values[5]));
		br.setToNodeId(getInt(values[6]));
		br.setProtocolType(getInt(values[7]));
		br.setTransDirection(getInt(values[8]));
		br.setResult(getInt(values[9]));
		br.setResultReason(values[10]);
		br.setRecvRequestTime(getLong(values[11]));
		br.setSendRequestTime(getLong(values[12]));
		br.setRecvResponseTime(getLong(values[13]));
		br.setSendResponseTime(getLong(values[14]));
		br.setRecvRequestLength(getInt(values[15]));
		br.setSendRequestLength(getInt(values[16]));
		br.setRecvResponseLength(getInt(values[17]));
		br.setSendResponseLength(getInt(values[18]));
		br.setResponseHost(values[19]);

		br.setResultCode(getInteger(values[20]));
		br.setServiceResultCode(getInteger(values[21]));

		br.setAnswerValidateCode(Integer.valueOf(getInt(values[22])));
		br.setValidateDesc(values[23]);
		return br;
	}

	private static int getInt(String value) {
		try {
			return Integer.valueOf(value).intValue();
		} catch (Exception localException) {
		}
		return -1;
	}

	private static Integer getInteger(String value) {
		try {
			return Integer.valueOf(value);
		} catch (Exception localException) {
		}
		return null;
	}

	private static long getLong(String value) {
		try {
			return Long.valueOf(value).longValue();
		} catch (Exception localException) {
		}
		return -1L;
	}

	@Transient
	public static String getSynSeq() {
		StringBuilder result = new StringBuilder(getSeq());
		int len = result.length();
		for (int i = len; i <= 8; i++) {
			result.insert(0, "0");
		}
		return Comutils.formatTime(System.currentTimeMillis(), "yyyyMMddHHmmssSSS") + result.toString();
	}

	@Transient
	private static synchronized int getSeq() {
		if (seq == 99999999) {
			seq = 0;
		}
		return ++seq;
	}
}
